#include "basic.h"
#include "struct.h"

//==============================================================================DATA
static FILE *m3u_fd = NULL;
static FILE *m3u_meta_fd = NULL;
static FILE *m3u_info_fd = NULL;

//==============================================================================FUNCTION
int m3u_open( char *name, char *how )
{
	assert( name && how );
	FILE* fd = NULL;
	
	char *name_buf = malloc( 512 );
	assert( name_buf );
	strcpy( name_buf, context.lst_dir );
	strcat( name_buf, name );
	
	strcat( name_buf, ".m3u" );
	if( NULL != (fd = fopen(name_buf, how)) ){
		m3u_fd = fd;
	}else{
		goto error;
	}

	strcat( name_buf, ".meta" );
	if( NULL != (fd = fopen(name_buf, how)) ){
		m3u_meta_fd = fd;
	}else{
		goto error;
	}
	
	strcat( name_buf, ".info" );
	if( NULL != (fd = fopen(name_buf, how)) ){
		m3u_info_fd = fd;
	}else{
		goto error;
	}
	
	return 0;
error:
	free( name_buf );
	m3u_fd = NULL;
	m3u_meta_fd = NULL;
	m3u_info_fd = NULL;
	return -1;
}

int m3u_close()
{
	if( m3u_fd ){ fclose( m3u_fd ); m3u_fd = 0; }
	if( m3u_meta_fd ){ fclose( m3u_meta_fd ); m3u_meta_fd = 0; }
	if( m3u_info_fd ){ fclose( m3u_info_fd ); m3u_info_fd = 0; }
	return 0;
}

int m3u_delete( char *name )
{
	assert( name );
	
	char *name_buf = malloc( 512 );
	assert( name_buf );
	strcpy( name_buf, context.lst_dir );
	strcat( name_buf, name );
	strcat( name_buf, ".m3u" );
	remove( name_buf );
	strcat( name_buf, ".meta" );
	remove( name_buf );
	strcat( name_buf, ".info" );
	remove( name_buf );
	
	free( name_buf );
	return 0;
}

int m3u_rename( char *old, char *new )
{
	assert( old && new );

	char *old_name = malloc( 512 );
	assert( old_name );
	strcpy( old_name, context.lst_dir );
	strcat( old_name, old );
	char *new_name = malloc( 512 );
	assert( new_name );
	strcpy( new_name, context.lst_dir );
	strcat( new_name, new );
	
	strcat( old_name, ".m3u" );
	strcat( new_name, ".m3u" );
	rename( old_name, new_name );
	strcat( old_name, ".meta" );
	strcat( new_name, ".meta" );
	rename( old_name, new_name );
	strcat( old_name, ".info" );
	strcat( new_name, ".info" );
	rename( old_name, new_name );

	free( old_name );
	free( new_name );
	return 0;
}

int m3u_insert()
{

	return 0;
}

//------------------------------------------------------------------------------
int m3u_info_write( entry_s *ety )
{
	assert( ety );
	
	fwrite( ety->name, strlen(ety->name), 1, m3u_fd);
	fwrite( "\n", 1, 1, m3u_fd);

	fwrite( ety, sizeof(entry_s), 1, m3u_info_fd );
	fprintf( m3u_info_fd, "%s%c", ety->name, '\0' );
	fprintf( m3u_info_fd, "%s%c", ety->file_name, '\0' );
	fprintf( m3u_info_fd, "%s%c", ety->artist, '\0' );
	fprintf( m3u_info_fd, "%s%c", ety->title, '\0' );	
	fprintf( m3u_info_fd, "%s%c", ety->album, '\0' );
	return 0;
}

int m3u_info_read( entry_s *ety )
{
	assert( ety );

	fread( ety, sizeof(entry_s), 1, m3u_info_fd);
//#if 0
	char temp[2048];
	int i = 0;
	do{
		fread( &temp[i], sizeof(char), 1, m3u_info_fd);
		if( temp[i] == '\0' ) break;
	}while( ++i );
	ety->name = (char *)malloc( i+1 );
	assert( ety->name );
	strcpy( ety->name, temp );

	i = 0;
	do{
		fread( &temp[i], sizeof(char), 1, m3u_info_fd);
		if( temp[i] == '\0' ) break;
	}while( ++i );
	ety->file_name = (char *)malloc( i+1 );
	assert( ety->file_name );
	strcpy( ety->file_name, temp );

	i = 0;
	do{
		fread( &temp[i], sizeof(char), 1, m3u_info_fd);
		if( temp[i] == '\0' ) break;
	}while( ++i );
	ety->artist = (char *)malloc( i+1 );
	assert( ety->artist );
	strcpy( ety->artist, temp );

	i = 0;
	do{
		fread( &temp[i], sizeof(char), 1, m3u_info_fd);
		if( temp[i] == '\0' ) break;
	}while( ++i );
	ety->title = (char *)malloc( i+1 );
	assert( ety->title );
	strcpy( ety->title, temp );

	i = 0;
	do{
		fread( &temp[i], sizeof(char), 1, m3u_info_fd);
		if( temp[i] == '\0' ) break;
	}while( ++i );
	ety->album = (char *)malloc( i+1 );
	assert( ety->album );
	strcpy( ety->album, temp );
//#endif
#if 0
	char temp[2048];
	fscanf( m3u_info_fd, "%s", temp);
	ety->name = (char *)malloc( strlen(temp)+1 );
	strcpy( ety->name, temp );

	fscanf( m3u_info_fd, "%s", temp);
	ety->file_name = (char *)malloc( strlen(temp)+1 );
	strcpy( ety->file_name, temp );
#endif	

	return 0;
}

int m3u_meta_write( list_s *list )
{
	assert( list );
	
	fwrite( list, sizeof(list_s), 1, m3u_meta_fd );
	
	fprintf( m3u_meta_fd, "%s%c", list->dir_name, '\0' );
	fprintf( m3u_meta_fd, "%s%c", list->list_name, '\0' );	
	fwrite( list->sequence, list->total_entries_number, sizeof(char *), m3u_meta_fd);
	
	return 0;
}

int m3u_meta_read( list_s *list )
{
	assert( list );
	
	fread( list, sizeof(list_s), 1, m3u_meta_fd );
//#if 0
	char temp[2048];
	int i = 0;
	do{
		fread( &temp[i], sizeof(char), 1, m3u_meta_fd);
		if( temp[i] == '\0' ) break;
	}while( ++i );
	list->dir_name = (char *)malloc( i+1 );
	assert( list->dir_name );
	strcpy( list->dir_name, temp );

	i = 0;
	do{
		fread( &temp[i], sizeof(char), 1, m3u_meta_fd);
		if( temp[i] == '\0' ) break;
	}while( ++i );
	list->list_name = (char *)malloc( i+1 );
	assert( list->list_name );
	strcpy( list->list_name, temp );
//#endif
#if 0
	char temp[2048];
	fscanf( m3u_info_fd, "%s", temp );
	list->dir_name = (char *)malloc( strlen(temp)+1 );
	strcpy( list->dir_name, temp );

	fscanf( m3u_info_fd, "%s", temp );
	list->list_name = (char *)malloc( strlen(temp)+1 );
	strcpy( list->list_name, temp );
#endif
	list->sequence = (unsigned int *)malloc( list->total_entries_number * sizeof(char *) );
	fread( list->sequence, list->total_entries_number, sizeof(char *), m3u_meta_fd);

	return 0;
}




	